import 'package:flutter/material.dart';
import 'package:jxchmz/API/%E5%BA%93%E5%AD%98%E7%AE%A1%E7%90%86/KuCunManager.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/KSProgressDialog.dart';
import 'package:get/get.dart';
class MaterialTypeManageUpdate extends StatefulWidget {
  @override
  _MaterialTypeManageUpdateState createState() => new _MaterialTypeManageUpdateState();
}

class _MaterialTypeManageUpdateState extends State<MaterialTypeManageUpdate> {
  //物料类别名称
  TextEditingController GoodsTypeNameController = new TextEditingController();

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  var isEnabled = true ;
  var argments ;
  var id;
//页面加载完成时
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    argments = Get.arguments['Order'];
    GoodsTypeNameController = new TextEditingController(text: argments['name']);
    if(argments['isEnabled']==0){
      isEnabled = false;
    }
    id = argments['id'];
    setState(() {
    });
  }

  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当 State 被暂时从视图树中移除时，会调用这个方法，同时页面切换时，也会调用。
  @override
  void deactivate() {
    // TODO: implement dispose
    super.deactivate();
  }

  ///在 initState() 之后调用，当 State 对象的依赖关系发生变化时，该方法被调用，初始化时也会调用。
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  // TODO: add state variables and methods
  @override
  Widget build(BuildContext context) {
    // TODO: add widget build method
    return  Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text("修改物料类别", style: TextStyle(fontSize: 15, color: Colors.black),),
        centerTitle: true,
        elevation: 0,
        iconTheme: IconThemeData(
            color: Colors.black
        ),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.only(left: 15,right: 15),
        child: Column(
          children: <Widget>[
            Form(
              key: _formKey,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                      hintText: 'MaterialTypeName'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.perm_identity),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                    controller: GoodsTypeNameController,
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'MaterialTypeName'.tr;
                      }
                      return null;
                    },
                  ),
                  SwitchListTile(
                      value: isEnabled,
                      title: const Text("是否启用"),
                      onChanged: (val) {
                        setState((){
                          isEnabled = val;
                        });
                      })
                ],
              ),
            ),


          ],
        ),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.all(15),
        child: RaisedButton(
          child: Text('Submitinformation'.tr,style: TextStyle(color: Colors.white),),
          onPressed: () {
            //一步校验全部表单
            if (_formKey.currentState!.validate()) {
              //提交信息
              UploadGoodsType();
            }
          },
          color: Colors.blue,
        ),
      ),
    );
  }
  UploadGoodsType() async {
    var IsEnabled = 1;
    if(!isEnabled){
      IsEnabled = 0;
    }
    //搜索参数
    var data = {
      "name": GoodsTypeNameController.text,
      "isEnabled": IsEnabled,
      "id":id
    };
    KSProgressDialog().LoadingToast("Submitting".tr, context);
    var ResponseData = await KuCunManager().UpdateGoodsType(data);
    KSProgressDialog().CloseLoading();
    if (ResponseData['code'] == 200) {
      GetxTools().SuccessToast("Thesubmissionwassuccessful".tr);
    } else {
      new GetxTools().ErrorToast("Commitfailed".tr);
    }
    setState(() {});
  }
}
